import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userInfo: {}
  },
  getters: {
    getUserInfo: sta => {
      return sta.userInfo
    },
    getUserInfoCache: sta => {
      if(!sta.userInfo) {
        let uif = sessionStorage.getItem('userInfo');
        if(uif) {
          return JSON.parse(uif);
        }
      }
      return sta.userInfo
    }
  },
  mutations: {
    setToken: (sta, token) => {
      sta.token = token;
      localStorage.setItem('token', token);
    },
    setUserInfo: (sta, userInfo) => {
      sta.userInfo = userInfo;
      sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    },
    removeTokenUserInfo: (sta) => {
      sta.token = ''
      sta.userInfo = {}
      localStorage.removeItem('token')
      sessionStorage.removeItem('userInfo')
    }
  },
  actions: {
  },
  modules: {
  }
})
